/**
 * jims
 */
package com.jims.master.modules.drug.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.drug.dao.DrugStorageDeptDao;
import com.jims.master.modules.drug.entity.DrugStorageDept;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.OrgDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 药品库存单位字典表Service
 *
 * @author DT
 * @version 2017-03-09
 */
@Service
@Transactional(readOnly = true)
public class DrugStorageDeptService extends CrudService<DrugStorageDeptDao, DrugStorageDept> {
    @Autowired
    private OrgDeptDao orgDeptDao;

    public DrugStorageDept get(String id) {
        return super.get(id);
    }

    public List<DrugStorageDept> findList(DrugStorageDept drugStorageDept) {
        return super.findList(drugStorageDept);
    }

    public Page<DrugStorageDept> findPage(Page<DrugStorageDept> page, DrugStorageDept drugStorageDept) {
        drugStorageDept.updateOrgId();
        return super.findPage(page, drugStorageDept);
    }

    @Transactional(readOnly = false)
    public int saveStorage(DrugStorageDept drugStorageDept){
        int i=0;
        OrgDept orgDept = new OrgDept();
        if(StringUtils.isBlank(drugStorageDept.getStorageName())) {
            if (StringUtils.isNotBlank(drugStorageDept.getStorageCode())) {//根据字节码判断是否做删除还是添加
                orgDept.setId(drugStorageDept.getStorageCode());
                orgDept.updateOrgId();
                orgDept = orgDeptDao.findId(orgDept);
                drugStorageDept.setStorageName(orgDept.getDeptName());//根据字节码从OrgDept找到StroagName
                drugStorageDept.setStorageCode(orgDept.getId());
            }
                drugStorageDept.preInsert();
                drugStorageDept.updateOrgId();
                drugStorageDept.setId(drugStorageDept.getStorageCode());
                i = dao.insert(drugStorageDept);

        }else{
            drugStorageDept.preUpdate();
            i=dao.update(drugStorageDept);
        }
        return i;
    }

//    @Transactional(readOnly = false)
//    public int save(DrugStorageDept drugStorageDept) {
//        int i = 0;
//        //先查询药品库存单位表中是否有数据，如果无直接做添加
//        DrugStorageDept drugStorageDept1 = dao.get(drugStorageDept);
//        if (drugStorageDept1 == null) {
//            drugStorageDept.updateOrgId();
//            i = dao.insert(drugStorageDept);
//        } else {
//            drugStorageDept.preUpdate();
//            i = dao.update(drugStorageDept);
//        }
//        return i;
//    }
    @Transactional(readOnly = false)
    public int delete(DrugStorageDept drugStorageDept) {
        return super.delete(drugStorageDept);
    }

    /**
     * 药品库存单位自动补全
     *
     * @param drugStorageDept
     * @param q
     * @param limit
     * @return
     * @author ZHUQ
     * @date 2017-03-12 15:08:59
     */
    public List<DrugStorageDept> findAutoCompleteList(DrugStorageDept drugStorageDept, String q, String limit) {
        drugStorageDept.updateOrgId();
        return dao.findAutoCompleteList(drugStorageDept, q, limit);
    }

    /**
     * 药品库存单位自动补全、根据入库方式自动选择是库房还是厂商
     *
     * @param drugStorageDept
     * @param q
     * @param limit
     * @return
     * @author ZHUQ
     * @date 2017-03-12 15:08:59
     */
    public List<DrugStorageDept> findByMasterClass(DrugStorageDept drugStorageDept, String q, String limit,String masterClass) {
        drugStorageDept.updateOrgId();
        List<DrugStorageDept> drugStorageDeptList = new ArrayList<DrugStorageDept>();
        if("采购".equals(masterClass)){
            drugStorageDeptList = dao.findAutoCompleteBySupplier(drugStorageDept, q, limit);
        }else{
            drugStorageDeptList = dao.findAutoCompleteList(drugStorageDept, q, limit);
        }
        return drugStorageDeptList;
    }

    /**
     * 根据科室ID查询库房信息
     * @param storageCode 库房表drug_storage_dept的storageCode,是科室表org_dept的ID
     * @return
     * @author fengyg
     */
    public DrugStorageDept getByStorageCode(String storageCode){
        return dao.getByStorageCode(storageCode);
    }

}